<?php
	class CategoryController extends Controller{
		private $model;
		private $category;
		public function __init(){
			$this->model=K('category');
			// $this->category=$this->model->getTreeCategory();
			$this->category=S('category');
			$this->cid=Q('cid',0,'intval');
			// p($this->category);die;
		}
		public function index(){
			$this->assign('category',$this->category);
			$this->display();
		}
		public function add(){

			if(IS_POST){
				if($this->model->addCategory()){
					// $this->model->catchData();
					$this->success('添加成功');
				}else{
					$this->error($this->model->error);
					// p($_POST);die;
				}
				// p($_POST);
			}else{
				$this->assign('category',$this->category);
				// p($this->category);
				$this->display();
			}	
		}
		public function edit(){
			if(IS_POST){
				if($this->model->saveCategory()){
					// $this->model->catchData();
					$this->success('修改成功');
				}else{
					$this->error($this->model->error);
				}
				
			}else{
				$data=$this->model->getCategoryByCid();
				$category = $this->model->getEditCategory();
				// p($category);die;
				$this->assign('data',$data);
				$this->assign('category',$category);
				// $this->assign('category')$this->category
				$this->display();
			}	
		}
		public function cache(){
			$re=$this->model->cacheData();
			if($re){
				$this->success('更新缓存成功');
			}else{
				$this->error('更新缓存失败');
			}
		}
		public function sort(){
			// p($_POST['cid']);die;
			// p($_POST);die;
			foreach ($_POST['cid'] as $k => $v) {
				$this->model->where(array('cid'=>$v))->save(array('sort'=>$_POST['sort'][$k]));
			}
			$this->model->cacheData();
			$this->success('排序成功');
		}

	}
?>